package cn.rkylin.oms.warehouse.stockGoods.controller;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.rkylin.oms.common.export.IExport;
import cn.rkylin.oms.warehouse.stock.vo.StockVO;
import cn.rkylin.oms.warehouse.stockGoods.vo.StockGoodsExportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.system.project.vo.ProjectVO;
import cn.rkylin.oms.system.stockpush.domain.StockFmla;
import cn.rkylin.oms.system.stockpush.service.IStockPushService;
import cn.rkylin.oms.system.storage.service.IStorService;
import cn.rkylin.oms.system.storage.vo.StorVO;
import cn.rkylin.oms.warehouse.stockGoods.dao.IStockGoodsDAO;
import cn.rkylin.oms.warehouse.stockGoods.service.IStockGoodsService;
import cn.rkylin.oms.warehouse.stockGoods.vo.StockGoodsVO;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * 商品库存汇总控制类
 *
 * @author zhangheng
 * @date 2018/01/23
 */
@Controller
@RequestMapping("/stockGoods")
public class StockGoodsController extends ApolloController {
    private static final Log logger = LogFactory.getLog(StockGoodsController.class);

    @Autowired
    public IStockGoodsService stockGoodsService;

    @Autowired
    private LogUtil logUtil;
    @Autowired
    private IStockPushService stockPushService;

    @Override
    public void afterPropertiesSet() throws Exception {
        setExportService((IExport) stockGoodsService);
    }
    /**
     * getStockGoodsInfo:根据查询条件获取库存信息列表. <br/>
     *
     * @param quickSearch
     * @param start
     * @param length
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getStockGoodsInfo")
    public Map<String, Object> getStockGoodsInfo(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                            @RequestParam(required = false, defaultValue = "10") int length ) throws Exception {
        logger.info("查询库存信息：/stockGoods/getStockGoodsInfo");
        Map<String, Object> returnMap = new HashMap<>();

        StockGoodsVO queryParamVO = new StockGoodsVO();

//        if (StringUtils.isBlank(quickSearch)) {
//            returnMap = getSuccessMap("查询库存信息成功");
//            returnMap.put(RECORDS_FILTERED, 0);
//            returnMap.put(RECORDS_TOTAL, 0);
//            returnMap.put(RETURN_DATA, new ArrayList<StockVO>());
//            return returnMap;
//        }
        // 获取当前登录人
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            return getFailedMap("查询当前登录人失败");
        }
        // 获取当前登录人所属项目
        String projectId = currentUser.getCurrentProject().getPrjId();
        if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
            return getFailedMap("查询当前登录人所属项目失败");
        }
        // 设置项目id
        queryParamVO.setPrjId(projectId);

        String entId = null;
        // 找前台选择的项目，对应的企业（这种做法是为了适应，用户对应到多个企业下的情况。）
        for (ProjectVO project : currentUser.getProjectMap().values()) {
            if (project.getPrjId().equalsIgnoreCase(projectId)) {
                entId = project.getEntId();
            }
        }
        queryParamVO.setEntId(entId);
        // 根据项目ID获取库存公式列表. 通常情况下，一个项目下只有一个库存计算公式
        StockFmla stockFmla = stockPushService.getStockFmlaByProjectId(projectId);

        // 快速查询
        if (quickSearch != null && StringUtils.isNotEmpty(quickSearch)) {
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            if (StringUtils.isNotEmpty(quickSearch)) {
                queryParamVO.setQuickSearch(quickSearch);
            }
        }

        Map<String, String> replaceFieldsMap = new HashMap<String, String>();
        if (null != stockFmla && "ext".equals(stockFmla.getStockValMode())) {
            replaceFieldsMap.put("available", "t.sell_count-t.allot_qty-t.freeze_qty-t.reserve_qty- t.unallot_qty ");
            replaceFieldsMap.put("canallotQty", "t.sell_count-t.allot_qty-t.freeze_qty-t.reserve_qty");
        } else {
            replaceFieldsMap.put("available", "t.stock_qty-t.allot_qty-t.freeze_qty-t.reserve_qty-t.logic_stockout_qty- t.unallot_qty ");
            replaceFieldsMap.put("canallotQty", "t.stock_qty-t.allot_qty-t.freeze_qty-t.reserve_qty-t.logic_stockout_qty");
        }
        // 排序语句生成
        String orderStatement = getOrderString(IStockGoodsDAO.class.getName(), "pageSelectStockGoods", replaceFieldsMap);
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        boolean orderFlag = true;
        for (int i = 0; orderFlag; i++) {
            // 排序的列号
            String order = request.getParameter("order[" + i + "][column]");
            // 排序的顺序asc or desc
            String orderDir = request.getParameter("order[" + i + "][dir]");
            // 排序的列名
            String orderColumn = request.getParameter("columns[" + order + "][data]");

            if (StringUtils.isEmpty(order) || StringUtils.isEmpty(orderDir) || StringUtils.isEmpty(orderColumn)) {
                orderFlag = false;
            } else {
                // 处理转义字段
                if (replaceFieldsMap != null && replaceFieldsMap.size() > 0) {
                    for (String replaceItem : replaceFieldsMap.keySet()) {
                        if (orderColumn.equals(replaceItem)) {
                            orderStatement = replaceFieldsMap.get(orderColumn)+" " + orderDir;
                            break;
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(orderStatement)) {
            queryParamVO.setOrderBy(orderStatement);
        }
        // 处理分页信息
        if (length == -1) {
            length = Integer.MAX_VALUE;
        }
        int page = start / length + 1;

        // 执行查询
        PageInfo<StockGoodsVO> stockGoodsVOList = stockGoodsService.queryStockGoodsList(page, length, queryParamVO);

        if (null != stockGoodsVOList) {
            if (null != stockFmla && "ext".equals(stockFmla.getStockValMode())) {
                for (int i = 0; i < stockGoodsVOList.getList().size(); i++) {
                    StockGoodsVO stockGoodsVO = stockGoodsVOList.getList().get(i);

                    // 可分派数 （正品）= 库存数-冻结数-预留数-已分派数
                    stockGoodsVO.setCanallotQty(stockGoodsVO.getSellCount() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty());
                    // 可分派数 （次品）= 库存数-已分派数
                    //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkCount() - stockGoodsVO.getJunkAllotQty());
                    // 可用数 （正品）= 可分派数 （正品）-未分派数
                    stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
                }
            } else {

                for (int i = 0; i < stockGoodsVOList.getList().size(); i++) {
                    StockGoodsVO stockGoodsVO = stockGoodsVOList.getList().get(i);

                    // 库存数使用本地计算：可分派数 = 库存数(stock_qty) - 已分派数- 预留数- 冻结数-逻辑出库数
                    stockGoodsVO.setCanallotQty(stockGoodsVO.getStockQty() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty()
                            - stockGoodsVO.getLogicStockoutQty());
                    // 可用数 （正品）= 可分派数-未分派数
                    stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
                    // 可分派数 （次品）= 库存数-已分派数
                    //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkQty() - stockGoodsVO.getJunkAllotQty());
                }
            }
            returnMap = getSuccessMap("查询库存信息成功");
            returnMap.put(RECORDS_FILTERED, stockGoodsVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, stockGoodsVOList.getTotal());
            returnMap.put(RETURN_DATA, stockGoodsVOList.getList());
        } else {
            returnMap = getFailedMap("查询库存信息失败");
        }
        logger.info("查询库存信息结束");
        return returnMap;
    }


    /**
     * getStockGoodsStorLocalInfo:查询商品各个仓库存储情况
     *
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getStockGoodsStorLocalInfo")
    public Map<String, Object> getStockGoodsStorLocalInfo(String goodsId) throws Exception {
        logger.info("查询商品本地仓库存储情况：/stockGoods/getStockGoodsStorLocalInfo");
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        StockGoodsVO queryParamVO = new StockGoodsVO();

        // 获取当前登录人
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            return getFailedMap("查询当前登录人失败");
        }
        // 获取当前登录人所属项目
        String projectId = currentUser.getCurrentProject().getPrjId();
        if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
            return getFailedMap("查询当前登录人所属项目失败");
        }
        // 设置项目id
        queryParamVO.setPrjId(projectId);

        String entId = null;
        // 找前台选择的项目，对应的企业（这种做法是为了适应，用户对应到多个企业下的情况。）
        for (ProjectVO project : currentUser.getProjectMap().values()) {
            if (project.getPrjId().equalsIgnoreCase(projectId)) {
                entId = project.getEntId();
            }
        }
        queryParamVO.setEntId(entId);
        // 根据项目ID获取库存公式列表. 通常情况下，一个项目下只有一个库存计算公式
        StockFmla stockFmla = stockPushService.getStockFmlaByProjectId(projectId);

        // 校验商品编码
        if (StringUtils.isBlank(goodsId)) {
            return getFailedMap("商品编码为空");
        } else {
            queryParamVO.setGoodsId(goodsId);
        }

        List<String> idArray = new ArrayList<String>();

        queryParamVO.setIdArray(idArray);

        Map<String, String> replaceFieldsMap = new HashMap<String, String>();
        String orderStatement = getOrderString(cn.rkylin.oms.warehouse.stockGoods.dao.IStockGoodsDAO.class.getName(), "selectStockGoodsLoca", replaceFieldsMap);

        if (StringUtils.isNotEmpty(orderStatement)) {
            queryParamVO.setOrderBy(orderStatement);
        }

        // 执行查询
        List<StockGoodsVO> list = stockGoodsService.getStockGoodsLocaStorInfo(queryParamVO);
        if (null != list) {
            returnMap = getSuccessMap("查询库存信息成功");
            returnMap.put(RECORDS_FILTERED, list == null ? 0 : list.size());
            returnMap.put(RECORDS_TOTAL, list.size());
            returnMap.put(RETURN_DATA, list);
        } else {
            returnMap = getFailedMap("查询库存信息失败");
        }
        logger.info("查询库存信息结束");
        return returnMap;
    }

    /**
     * getStockGoodsStorExtInfo:查询商品各个仓库存储情况
     *
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getStockGoodsStorExtInfo")
    public Map<String, Object> getStockGoodsStorExtInfo(String goodsId) throws Exception {
        logger.info("查询商品第三方仓库存储情况：/stockGoods/getStockGoodsStorExtInfo");
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        StockGoodsVO queryParamVO = new StockGoodsVO();

        // 获取当前登录人
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            return getFailedMap("查询当前登录人失败");
        }
        // 获取当前登录人所属项目
        String projectId = currentUser.getCurrentProject().getPrjId();
        if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
            return getFailedMap("查询当前登录人所属项目失败");
        }
        // 设置项目id
        queryParamVO.setPrjId(projectId);

        String entId = null;
        // 找前台选择的项目，对应的企业（这种做法是为了适应，用户对应到多个企业下的情况。）
        for (ProjectVO project : currentUser.getProjectMap().values()) {
            if (project.getPrjId().equalsIgnoreCase(projectId)) {
                entId = project.getEntId();
            }
        }
        queryParamVO.setEntId(entId);

        // 根据项目ID获取库存公式列表. 通常情况下，一个项目下只有一个库存计算公式
        StockFmla stockFmla = stockPushService.getStockFmlaByProjectId(projectId);

        // 校验商品编码
        if (StringUtils.isBlank(goodsId)) {
            return getFailedMap("商品编码为空");
        } else {
            queryParamVO.setGoodsId(goodsId);
        }

        List<String> idArray = new ArrayList<String>();

        queryParamVO.setIdArray(idArray);

        Map<String, String> replaceFieldsMap = new HashMap<String, String>();
        String orderStatement = getOrderString(cn.rkylin.oms.warehouse.stockGoods.dao.IStockGoodsDAO.class.getName(), "selectStockGoodsExt", replaceFieldsMap);

        if (StringUtils.isNotEmpty(orderStatement)) {
            queryParamVO.setOrderBy(orderStatement);
        }

        // 执行查询
        List<StockGoodsVO> list = stockGoodsService.getStockGoodsExtStorInfo(queryParamVO);
        if (null != list) {
//            if (null != stockFmla && "ext".equals(stockFmla.getStockValMode())) {
//                for (int i = 0; i < list.size(); i++) {
//                    StockGoodsVO stockGoodsVO = list.get(i);
//
//                    // 可分派数 （正品）= 库存数-冻结数-预留数-已分派数 6
//                    stockGoodsVO.setCanallotQty(stockGoodsVO.getSellCount() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty());
//
//                    // 可用数 （正品）= 可分派数 （正品）-未分派数
//                    stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
//                }
//            }
//            else {
//                for (int i = 0; i < list.size(); i++) {
//                    StockGoodsVO stockGoodsVO = list.get(i);
//
//                    // 可分派数 （正品）= 库存数-冻结数-预留数-已分派数 6
//                    stockGoodsVO.setCanallotQty(stockGoodsVO.getSellCount() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty()- stockGoodsVO.getLogicStockoutQty());
//
//                    // 可用数 （正品）= 可分派数 （正品）-未分派数
//                    stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
//                }
//            }
            returnMap = getSuccessMap("查询库存信息成功");
            returnMap.put(RECORDS_FILTERED, list == null ? 0 : list.size());
            returnMap.put(RECORDS_TOTAL, list.size());
            returnMap.put(RETURN_DATA, list);
        } else {
            returnMap = getFailedMap("查询库存信息失败");
        }
        logger.info("查询库存信息结束");
        return returnMap;
    }


    private Map<String, Object> getSuccessMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, SUCCESS);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };

    private Map<String, Object> getFailedMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, FAILED);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };

    /**
     * 验证需要导出的条件
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/exportCheckStock")
    public Map<String, Object> exportCheckStock() throws Exception {
        logger.info("库存汇总导出check：/stockGoods/exportCheckStock");
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        try {

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null){
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "查询当前登录人失败");
                return returnJSON;
            }

            // 获取当前登录人所属项目
            String projectId = currentUser.getCurrentProject().getPrjId();
            if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "查询当前登录人所属项目失败");
                return returnJSON;
            }

            String entId = null;
            // 找前台选择的项目，对应的企业（这种做法是为了适应，用户对应到多个企业下的情况。）
            for (ProjectVO project : currentUser.getProjectMap().values()) {
                if (project.getPrjId().equalsIgnoreCase(projectId)) {
                    entId = project.getEntId();
                }
            }

            if (StringUtils.isBlank(entId)){
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "查询当前登录人所属企业失败");
                return returnJSON;
            }

            // 上述check都没有错误的场合
            if (returnJSON.get(JSON_RESULT) == null) {
                returnJSON.put(JSON_RESULT, SUCCESS);
            }

        } catch (Exception ex){
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }
        return returnJSON;
    }

    /**
     * 导出
     * @param colName
     * @param colValue
     * @param quickSearch
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/exportStock")
    public String exportStock(String colName, String colValue, String quickSearch, String storType) throws Exception {
        logger.info("查询库存汇总信息导出：/stockGoods/exportStock");
        try {
            StockGoodsVO queryParamVO = new StockGoodsVO();
            Map<String,String> map = new HashMap<String,String>();

            // 获取当前登录人
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                logger.error("查询库存汇总信息导出：/stockGoods/exportStock,查询当前登录人失败");
                return null;
            }
            // 获取当前登录人所属项目
            String projectId = currentUser.getCurrentProject().getPrjId();
            if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
                logger.error("查询库存汇总信息导出：/stock/exportStock,查询当前登录人所属项目失败");
                return null;
            }
            // 设置项目id
            queryParamVO.setPrjId(projectId);
            map.put("prjId",projectId);

            String entId = null;
            // 找前台选择的项目，对应的企业（这种做法是为了适应，用户对应到多个企业下的情况。）
            for (ProjectVO project : currentUser.getProjectMap().values()) {
                if (project.getPrjId().equalsIgnoreCase(projectId)) {
                    entId = project.getEntId();
                }
            }
            queryParamVO.setEntId(entId);
            map.put("entId",entId);
            // 根据项目ID获取库存公式列表. 通常情况下，一个项目下只有一个库存计算公式
            StockFmla stockFmla = stockPushService.getStockFmlaByProjectId(projectId);

            // 快速查询
            if (quickSearch != null && StringUtils.isNotEmpty(quickSearch)) {
                quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
                if (StringUtils.isNotEmpty(quickSearch)) {
                    queryParamVO.setQuickSearch(quickSearch);
                    map.put("quickSearch",quickSearch);
                }
            }

            String exportStatement = "";
            if ("p".equals(storType)){
                // 执行查询
                PageInfo<StockGoodsVO> stockGoodsVOList = stockGoodsService.queryStockGoodsList(0, Integer.MAX_VALUE, queryParamVO);

                if (null != stockGoodsVOList) {
                    if (null != stockFmla && "ext".equals(stockFmla.getStockValMode())) {
                        for (int i = 0; i < stockGoodsVOList.getList().size(); i++) {
                            StockGoodsVO stockGoodsVO = stockGoodsVOList.getList().get(i);

                            // 可分派数 （正品）= 库存数-冻结数-预留数-已分派数
                            stockGoodsVO.setCanallotQty(stockGoodsVO.getSellCount() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty());
                            // 可分派数 （次品）= 库存数-已分派数
                            //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkCount() - stockGoodsVO.getJunkAllotQty());
                            // 可用数 （正品）= 可分派数 （正品）-未分派数
                            stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
                        }
                    } else {

                        for (int i = 0; i < stockGoodsVOList.getList().size(); i++) {
                            StockGoodsVO stockGoodsVO = stockGoodsVOList.getList().get(i);

                            // 库存数使用本地计算：可分派数 = 库存数(stock_qty) - 已分派数- 预留数- 冻结数-逻辑出库数
                            stockGoodsVO.setCanallotQty(stockGoodsVO.getStockQty() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty()
                                    - stockGoodsVO.getLogicStockoutQty());
                            // 可用数 （正品）= 可分派数-未分派数
                            stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
                            // 可分派数 （次品）= 库存数-已分派数
                            //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkQty() - stockGoodsVO.getJunkAllotQty());
                        }
                    }
                    exportExcelFile(stockGoodsVOList.getList(), colName, colValue, "库存查询导出");
                }
            } else {
                List<StockGoodsExportVO> list = stockGoodsService.getExportList(map,"ExportSelectStockGoods");
                if (null != list) {
//                    if (null != stockFmla && "ext".equals(stockFmla.getStockValMode())) {
//                        for (int i = 0; i < list.size(); i++) {
//                            StockGoodsExportVO stockGoodsVO = list.get(i);
//
//                            // 可分派数 （正品）= 库存数-冻结数-预留数-已分派数
//                            stockGoodsVO.setCanallotQty(stockGoodsVO.getSellCount() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty());
//                            // 可分派数 （次品）= 库存数-已分派数
//                            //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkCount() - stockGoodsVO.getJunkAllotQty());
//                            // 可用数 （正品）= 可分派数 （正品）-未分派数
//                            stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
//                        }
//                    } else {
//
//                        for (int i = 0; i < list.size(); i++) {
//                            StockGoodsExportVO stockGoodsVO = list.get(i);
//
//                            // 库存数使用本地计算：可分派数 = 库存数(stock_qty) - 已分派数- 预留数- 冻结数-逻辑出库数
//                            stockGoodsVO.setCanallotQty(stockGoodsVO.getStockQty() - stockGoodsVO.getAllotQty() - stockGoodsVO.getFreezeQty() - stockGoodsVO.getReserveQty()
//                                    - stockGoodsVO.getLogicStockoutQty());
//                            // 可用数 （正品）= 可分派数-未分派数
//                            stockGoodsVO.setAvailable(stockGoodsVO.getCanallotQty() - stockGoodsVO.getUnallotQty());
//                            // 可分派数 （次品）= 库存数-已分派数
//                            //stockGoodsVO.setJunkCanallotQty(stockGoodsVO.getJunkQty() - stockGoodsVO.getJunkAllotQty());
//                        }
//                    }
                    exportExcelFile(list,colName,colValue,"库存查询导出");
                }

            }

        } catch (Exception ex){
            ex.printStackTrace();
            logger.error(ex.getMessage());
        }
        return null;
    }
}